package com.stn.zjx.givingtreasure.activity.msg;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.set.MsgSetActivity;
import com.stn.zjx.givingtreasure.adapter.MsgTypeListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.MsgTypeListDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.MsgNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.vondear.rxtools.RxActivityTool;
import com.zyyoona7.popup.EasyPopup;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MsgTypeListActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.twinkling_refresh)
    TwinklingRefreshLayout twinklingRefresh;


    @BindArray(value = R.array.msg_list)
    public String[] msg_arr;


    int pagenow = 1;
    int pagesize = 10;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    private boolean isMoreLoading = false;
    private int visibleItemCount = 0;
    private LinearLayoutManager lm;
    private MsgTypeListAdapter adapter;
    EasyPopup msgSetPop;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }


    @Override
    public void initComponent() {
        actionTitle.setText(getString(R.string.msg) + "(0)");
        actionAddBt.setVisibility(View.VISIBLE);
        backBt.setVisibility(View.VISIBLE);

        ProgressLayout headerView = new ProgressLayout(this);
        headerView.setColorSchemeResources(R.color.colorAccent);
        twinklingRefresh.setHeaderView(headerView);

        rv.setLayoutManager(lm = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false));
        rv.setAdapter(adapter = new MsgTypeListAdapter(this));
        twinklingRefresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                initRefresh();
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {

            }
        });

        twinklingRefresh.startRefresh();

//        rv.setOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (isMoreLoading == false && newState == RecyclerView.SCROLL_STATE_IDLE && visibleItemCount == adapter.dataBeanList.size() - 1 &&
//                        adapter.dataBeanList.size() >= pagenow * 10) {
//                    isMoreLoading = true;
//                    initAppend();
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                visibleItemCount = lm.findLastVisibleItemPosition();
//            }
//        });
        //初始化popu弹窗
        msgSetPop = EasyPopup.create()
                .setContentView(this, R.layout.popup_msg_type)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(true)
                .apply();


        RelativeLayout go_user_set = msgSetPop.findViewById(R.id.go_user_set);
        go_user_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(MsgTypeListActivity.this, MsgSetActivity.class);
                msgSetPop.dismiss();
            }
        });

        msgSetPop.getPopupWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            msgSetPop.getPopupWindow().setElevation(20);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_type_list);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt, R.id.action_add_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.action_add_bt:
                msgSetPop.showAsDropDown(actionAddBt, -200, 0);
                break;

        }
    }

    public void initRefresh() {
        if (UserService.getInstance(this).getOnlineLoginUser() == null) {
            twinklingRefresh.finishRefreshing();
            return;
        }
        MsgNetImpl
                .query_msg_type_list(UserService.getInstance(this).getOnlineLoginUser().getToken(),
                        UserService.getInstance(this).getOnlineLoginUser().getUserId() + "_query_msg_type_list",
                        new StringCallback() {
                            @Override
                            public void onFinish() {
                                super.onFinish();
                                twinklingRefresh.finishRefreshing();
                            }

                            @Override
                            public void onCacheSuccess(Response<String> response) {
                                super.onCacheSuccess(response);
                                if(response.isSuccessful()&&!isFinishing()){
                                    Gson gson = new Gson();
                                    MsgTypeListDomain domain = gson.fromJson(response.body(), MsgTypeListDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().getData() != null && domain.getData().getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData().getData());
                                            } else {
                                                adapter.clearList();
                                                noDataTv.setVisibility(View.VISIBLE);
                                            }
                                            actionTitle.setText(getString(R.string.msg) + "(" + domain.getData().getAll_count() + ")");
                                            break;
                                        default:
                                            adapter.clearList();
                                            noDataTv.setVisibility(View.VISIBLE);
                                            break;

                                    }
                                }



                            }

                            @Override
                            public void onSuccess(Response<String> response) {
                                if(response.isSuccessful()&&!isFinishing()){

                                    Gson gson = new Gson();
                                    MsgTypeListDomain domain = gson.fromJson(response.body(), MsgTypeListDomain.class);
                                    switch (domain.getStatus()) {
                                        case BaseApi.REQUEST_SUCCESS_CODE:
                                            if (domain.getData() != null && domain.getData().getData() != null && domain.getData().getData().size() != 0) {
                                                noDataTv.setVisibility(View.GONE);
                                                adapter.refreshList(domain.getData().getData());
                                            } else {
                                                adapter.clearList();
                                                noDataTv.setVisibility(View.VISIBLE);
                                            }
                                            actionTitle.setText(getString(R.string.msg) + "(" + domain.getData().getAll_count() + ")");
                                            break;
                                        default:
                                            adapter.clearList();
                                            noDataTv.setVisibility(View.VISIBLE);
                                            break;
                                    }

                                }



                            }
                        });


    }

}
